﻿#include "dialog_can_setting.h"
#include "com_manager.h"
#include "plug_info.h"
#include "ui_dialog_can_setting.h"

#include <nuf_bus_info.h>



#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif




DialogCanSetting::DialogCanSetting(int port, int board, QWidget* parent)
	: QDialog(parent)
	, ui(new Ui::DialogCanSetting)
    , port_id(port)
    , board_id(board)
{
	ui->setupUi(this);

	this->initWnd();


    this->setWindowTitle("Can 设置：" +  QString("node%1_p%2").arg(board).arg( port ) );

	//this->setWindowTitle(QString("Can"));
	//this->resize(960, 500);


}

DialogCanSetting::~DialogCanSetting()
{
	delete ui;
}


void DialogCanSetting::initWnd()
{
	// ReceiveCount = 0;
	// SendCount = 0;
	// IsShow = true;
	// IsAutoClear = false;
	// IsHexSend = true;
	// IsHexReceive = true;
	// IsDebug = false;

     QStringList comList;//串口号
	QStringList baudList;//波特率
	QStringList parityList;//校验位
	QStringList dataBitsList;//数据位
	QStringList stopBitsList;//停止位


    // enumerator = new QextSerialEnumerator(this);
    // enumerator->setUpNotifications();

    // connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
    // connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));

    foreach(QString info, com_manager::instance()->comList)
	{
        if (info.isEmpty() != false)
            comList.append(info );
	}

    ui->cboxPortName->addItems(comList);

	baudList << "1200" << "1800" << "2400" << "4800" << "9600"
		<< "19200" << "38400" << "56000" << "57600"
		<< "115200" << "128000" << "921600";

    ui->cboxBaudRate->addItems(baudList);

	parityList << "无" << "奇" << "偶";

#ifdef Q_OS_WIN//如果是windows系统
	parityList << "标志";
#endif

	parityList << "空格";

    ui->cboxParity->addItems(parityList);

	dataBitsList << "5" << "6" << "7" << "8";
    ui->cboxDataBit->addItems(dataBitsList);

	stopBitsList << "1";

#ifdef Q_OS_WIN//如果是windows系统
	stopBitsList << "1.5";
#endif

	stopBitsList << "2";

    ui->cboxStopBit->addItems(stopBitsList);

    QString node =  NufBusSetting::map_bus[board_id-1].p_list[port_id-1];
    _CAN p =  PlugInfo::map_can[node] ;
    ui->cboxPortName->setCurrentText(p.com );//串口号
    ui->cboxBaudRate->setCurrentText(p.baud);//波特率
    ui->cboxParity->setCurrentText(p.parity);//校验位
    ui->cboxDataBit->setCurrentText(p.dataBit);//数据位
    ui->cboxStopBit->setCurrentText(p.stopBit);//停止位



}

void DialogCanSetting::on_button_cancel_clicked()
{
    // _CAN p;
    // p.  com =ui->cboxPortName->currentText() ;//串口号
    // p.  baud = ui->cboxBaudRate->currentText();//波特率
    // p.  parity = ui->cboxParity->currentText();//校验位
    // p.dataBit = ui->cboxDataBit->currentText();//数据位
    // p.  stopBit = ui->cboxStopBit->currentText();//停止位

    // QString node =  NodeSetting::map_bus[board_id-1].p_list[port_id-1];
    // NodeSetting::map_can[node]  = p;
    // NodeSetting::saveNodeSetting();

	reject();
}



void DialogCanSetting::on_button_ok_clicked()
{
    _CAN p;
    p.  com =ui->cboxPortName->currentText() ;//串口号
    p.  baud = ui->cboxBaudRate->currentText();//波特率
    p.  parity = ui->cboxParity->currentText();//校验位
    p.dataBit = ui->cboxDataBit->currentText();//数据位
    p.  stopBit = ui->cboxStopBit->currentText();//停止位

    QString node =  NufBusSetting::map_bus[board_id-1].p_list[port_id-1];
    PlugInfo::map_can[node]  = p;
    PlugInfo::save_port_Setting() ;

    accept();
}

